package com.wy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wy.entity.ShopCart;
import com.wy.mapper.DrugMapper;
import com.wy.mapper.ShopCartMapper;
import com.wy.service.IShopCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王昭贵
 * @since 2023-01-27
 */
@Service
public class ShopCartServiceImpl extends ServiceImpl<ShopCartMapper, ShopCart> implements IShopCartService {

    @Resource
    private ShopCartMapper shopCartMapper;
    @Resource
    private DrugMapper drugMapper;

    @Override
    public List<ShopCart> getByCustomerId(Integer id) {
        List<ShopCart> list = shopCartMapper.selectList(new LambdaQueryWrapper<ShopCart>().eq(ShopCart::getCustomerId, id));
        list.stream().forEach(s -> s.setDrug(drugMapper.selectById(s.getDrugId())));
        return list;
    }

    /**
     * 加入购物车
     * @param shopCart
     * @return
     */
    @Override
    public int addShopCart(ShopCart shopCart) {

        ShopCart temp = shopCartMapper.selectOne(new LambdaQueryWrapper<ShopCart>().eq(ShopCart::getDrugId, shopCart.getDrugId()));
        if (temp == null) {
            return shopCartMapper.insert(shopCart);
        }
        temp.setCount(temp.getCount() + shopCart.getCount());
        return shopCartMapper.updateById(temp);
    }
}
